package com.choudou5.solr.analyzer.tokenizer;

import com.choudou5.solr.analyzer.utils.PinyinUtil;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

/**
 * <pre>
 * 描述：分隔符 分词器 
 * 构建组：solr.analyzer 
 * 作者：choudou5
 * 邮箱: hsolr@hotmail.com
 * 日期:Jul 15, 2015-3:18:00 PM 
 * </pre>
 */
public class PinyinTokenizer extends Tokenizer {

	private int dataLen = 0;
	private int dex = 0;
	private int increment = 0; //增长值
	private int wordCount = 0;
	private List<String> words;

	private final char[] ioBuffer = new char[512];
	private final CharTermAttribute termAtt = (CharTermAttribute) addAttribute(CharTermAttribute.class);
	private final OffsetAttribute offsetAtt = (OffsetAttribute) addAttribute(OffsetAttribute.class);

	public PinyinTokenizer(Reader in) {
		super(in);
	}

	public boolean incrementToken() throws IOException {
		clearAttributes();
		if (this.dataLen == 0) {
			this.dataLen = this.input.read(this.ioBuffer);
			words = PinyinUtil.getPinyinArray(ioBuffer);
			wordCount = words.size();
		}

		if (this.dataLen == -1) {
			return false;
		} else if (dex == wordCount) {
			return false;
		}
		String word = words.get(dex);
		int len = word.length();
		this.termAtt.append(word);
		this.offsetAtt.setOffset(correctOffset(increment), correctOffset(increment+len-1));
		
		increment += len;
		dex++;
		return true;
	}

	public void reset() throws IOException {
		super.reset();
		this.offsetAtt.setOffset(0, 0);
	}

}
